package com.ny.dao.impl;

import com.ny.dao.ProductDao;
import com.ny.entity.Product;
import com.ny.until.DbUntil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    DbUntil dbUntil = new DbUntil();

    @Override
    public List<Product> findAllProduct() throws SQLException, ClassNotFoundException {
        Connection con = dbUntil.getCon();
        String sql = "select * from products";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Product> productsList = new ArrayList<Product>();
        while (resultSet.next()) {
            Product product = new Product();
            int id = resultSet.getInt("product_id");
            product.setProductId(id);
            product.setProductName(resultSet.getString("name"));
            product.setPrice(resultSet.getDouble("price"));
            product.setStock(resultSet.getInt("stock"));
            product.setCategory(resultSet.getString("category"));
            product.setDescription(resultSet.getString("description"));
            productsList.add(product);
            return productsList;
        }
        return productsList;
    }
}
